# /usr/bin/python3

# Author: 爱编程的章老师
# @Time: 2021/1/7 0007
# E-mail: Bluesand2010@163.com
import pygame, sys, time

from random import randint


def homework():
    '''作业'''
    '''
    挡板接球
    一个宽100 ,高10的棕色挡板,一个直径20的小球
    小球从档板上方纵向距离200以上,左右位置随机的位置处以每次2单位,0.01秒一次的速度从上往下掉
    用左右方向键(a 和 d也可以)来控制挡板左右移动来接住小球.
    接住小不球,游戏胜利,
    未接住,游戏失败.
    未接住定义:当注球的最下方位于与挡板的最上方.3 个以上单位的时候
    接住定义:当小球的圆心横坐标在挡板左右之间,小球与挡板相交,且小球的下方-挡板上方<=3
    '''
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    # 挡板矩形
    board = pygame.Rect(350, 550, 100, 10)
    board_color = 139, 90, 43
    pygame.draw.rect(s, board_color, board)

    # 小球矩形
    bx = randint(0, 780)
    ball = pygame.Rect(bx, 100, 20, 20)
    ball_color = 255, 255, 255
    pygame.draw.ellipse(s, ball_color, ball)

    pygame.display.update()
    pygame.key.set_repeat(pygame.KEYDOWN)
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        ball.move_ip(0, 2)
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            board.move_ip(-5, 0)
        elif keys[pygame.K_RIGHT]:
            board.move_ip(5, 0)

        if ball.bottom - board.top > 5:
            print("你失败了,游戏结束")
            break
        if ball.colliderect(board):
            print("你胜利了")
            break

        s.fill((0, 0, 0))
        pygame.draw.rect(s, board_color, board)
        pygame.draw.ellipse(s, ball_color, ball)
        pygame.display.update()
        time.sleep(0.01)

# 组合按键响应2
def combination_key():
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LALT] or keys[pygame.K_RALT] and keys[pygame.K_F4]:
            sys.exit()


# 组合按键响应2
def combination_key2():
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_u] and keys[pygame.K_i]:
            print("我发动了大招,就问你怕不怕")

# 鼠标单击事件
def mouse_click():
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    print("左键单击")
                elif event.button == 2:
                    print("按下滚轮键")
                elif event.button == 3:
                    print("按下右键")
                elif event.button == 4:
                    print("滚轮朝上")
                elif event.button == 5:
                    print("按下朝下")
                print("鼠标当前的位置:", event.pos)

# 鼠标移动
def mouse_motion():
    pygame.init()
    s = pygame.display.set_mode((800, 600))

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEMOTION:
                print(event.pos)
                print(event.rel)
                print(event.buttons)


# 鼠标松开事件
def mouse_up():
    pygame.init()
    s = pygame.display.set_mode((800, 600))
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONUP:
                print(event.button)
                print(event.pos)

def mouse_drag():
    '''鼠标左键拖拽效果'''
    pygame.init()
    s = pygame.display.set_mode((800, 600))
    start_pos = 0, 0
    end_pos = 0, 0
    drag_flag = False
    width, height = 0, 0
    rect_color = GREEN = 0, 255, 0
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
                drag_flag = False
                s.fill((0,0,0))
                pygame.display.update()
            if not drag_flag and event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                drag_flag = True
                start_pos = event.pos
            if drag_flag and event.type == pygame.MOUSEMOTION:
                end_pos = event.pos
                if start_pos[0]  > end_pos[0]:
                    left = end_pos[0]
                else:
                    left = start_pos[0]
                if start_pos[1] > end_pos[1]:
                    top = end_pos[1]
                else:
                    top = start_pos[1]
                width = abs(start_pos[0] - end_pos[0])
                height = abs(start_pos[1] - end_pos[1])
                s.fill((0,0,0))
                pygame.draw.rect(s, rect_color, (left, top, width, height), 1)
                pygame.display.update()




if __name__ == '__main__':

    mouse_drag()
